from time import time
from scipy.stats import randint
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import RandomizedSearchCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_digits

print('=============程序开始执行================')

digits = load_digits()
X, y = digits.data, digits.target
print('1. 数据集加载完毕')

RFC = RandomForestClassifier(n_estimators=20)
print('2. 模型创建成功')

print('=============随机搜索调参================')
# 随机搜索/Randomized Search 构建候选参数分布
param_dist = {"max_depth": [3, 5],
              "max_features": randint(1, 11),
              "min_samples_split": randint(2, 11),
              "criterion": ["gini", "entropy"]}
n_iter_search = 20
random_search = RandomizedSearchCV(RFC, param_distributions=param_dist, n_iter=n_iter_search, cv=5)
start = time()
random_search.fit(X, y)
print('3. 随机搜索调参模型学习完毕')

print("RandomizedSearchCv took %.2f seconds for %d candidates，parameter settings." % ((time() - start), n_iter_search))
print("Best Parameters:"+str(random_search.best_params_))
print("Best Score: {:.4f}".format(random_search.best_score_))


print('=============网格搜索调参================')
# 网格搜索/Grid Search 构建候选参数网格
param_grid = {"max_depth": [3, 5], "max_features": [1, 3, 10], "min_samples_split": [2, 3, 10],
              "criterion": ["gini", "entropy"]}
grid_search = GridSearchCV(RFC, param_grid=param_grid, cv=5)
start = time()
grid_search.fit(X, y)
print('3. 网格搜索调参模型学习完毕')
print("GridSearchCV took %.2f seconds for %d candidate parameter settings." % (
    time() - start, len(grid_search.cv_results_['params'])))
print("Best Parameters:"+str(random_search.best_params_))
print("Best Score: {:.4f}".format(random_search.best_score_))


'''
结论：
运行结果里：

第一行输出每种追踪法运行的多少次和花的时间。
第二行输出最佳超参数的组合。
第三行输出最高得分。
在本例中，随机搜索比网格搜索用更短时间内找到一组超参数，获得了更高的得分。
'''